package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.AppVersion;
import com.dingreading.cloud.admin.entity.table.AppVersionTableDef;
import com.dingreading.cloud.admin.mapper.AppVersionMapper;
import com.dingreading.cloud.admin.service.AppVersionService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.mybatisflex.core.query.QueryMethods.max;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-07-31
 */
@Service
public class AppVersionServiceImpl extends BaseServiceImpl<AppVersionMapper, AppVersion, AppVersionTableDef> implements AppVersionService {

    @Override
    protected AppVersionTableDef getTable() {
        return AppVersionTableDef.appVersion;
    }

    @Override
    public Page<AppVersion> pageList(PageUtil pageUtil, Integer os, Integer forceStatus, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (null != os)
            condition.and(table.os.eq(os));
        if (null != forceStatus)
            condition.and(table.forceStatus.eq(forceStatus));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .from(table)
                .orderBy(table.id.desc())
                .where(condition);

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), queryWrapper);
    }

    @Override
    public int dropBean(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }

    @Override
    public List<AppVersion> getMaxCodeByOsAndVersionCode(Integer os, Integer versionCode, boolean selfStatus) {
        QueryCondition condition1 = table.os.eq(os)
                .and(table.enabled.eq(1))
                .and(table.versionCode.gt(versionCode));
        QueryCondition condition2 = table.os.eq(os)
                .and(table.enabled.eq(1))
                .and(table.versionCode.gt(versionCode));
        if (selfStatus) {
            condition1.and(table.selfStatus.in(1, 2));
            condition2.and(table.selfStatus.in(1, 2));
        } else {
            condition1.and(table.selfStatus.in(1, 3));
            condition2.and(table.selfStatus.in(1, 3));
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select(max(table.versionCode))
                .from(table)
                .where(condition1);

        QueryWrapper wrapper2 = QueryWrapper.create()
                .from(table)
                .where(condition2.and(table.versionCode.eq(wrapper)));

        return getMapper().selectListByQuery(wrapper2);
    }

    @Override
    public int updateEnable(Long id, Integer enabled, Date time, String uid, String userName) {
        AppVersion version = UpdateEntity.of(AppVersion.class);

        UpdateWrapper<AppVersion> wrapper = UpdateWrapper.of(version);
        wrapper.set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(uid))
                .set(table.lastStaffName, NepUtil.nullToZero(userName));

        return getMapper().updateByCondition(version, table.id.eq(id));
    }

    @Override
    public boolean haveVersionCodeOrName(Long id, Integer os, Integer versionCode, String versionName) {
        QueryCondition condition = table.os.eq(os)
                .and(table.versionCode.eq(versionCode).or(table.versionName.eq(versionName)));
        if (null != id)
            condition.and(table.id.ne(id));

        return exists(condition);
    }
}